package sg.edu.nus.iss.vmcs.customer;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import sg.edu.nus.iss.vmcs.abstraction.*;
import sg.edu.nus.iss.vmcs.util.LabelledDisplay;

/*
 * @author nyi nyi than
 */
public class CustomerPanel extends Frame implements ICustomerPanel
{
    private static final long serialVersionUID = 1L;

    private CustomerPanelPresenter presenter;

    private static final String title = "VIMTO Soft Drink Dispenser";

    private CoinInputBox coinInputBox;
    private DrinkSelectionBox drinkSelectionBox;
    private Label changeStatusLabel;
    private Button terminateButton;
    private LabelledDisplay collectCoinLabel;
    private LabelledDisplay collectCanLabel;

    public CustomerPanel(CustomerPanelPresenter presenter)
    {
	this.presenter = presenter;
	this.setTitle("VMCS - Customer Panel");

	Label titleLabel = new Label(title, Label.CENTER);
	titleLabel.setFont(new Font("Helvetica", Font.BOLD, 16));
	titleLabel.setBackground(Color.blue);
	titleLabel.setForeground(Color.yellow);
	Panel titlePanel = new Panel();
	titlePanel.setSize(20, 30);
	titlePanel.setLayout(new BorderLayout());
	titlePanel.add(titleLabel);

	Panel topPanel = new Panel();
	topPanel.setLayout(new GridLayout(0, 1, 0, 10));

	initialize();

	Panel belowPanel = new Panel();
	belowPanel.setLayout(new GridLayout(0, 1));
	belowPanel.add(changeStatusLabel);
	belowPanel.add(terminateButton);
	belowPanel.add(collectCoinLabel);
	belowPanel.add(collectCanLabel);

	topPanel.add(titlePanel);
	topPanel.add(coinInputBox);
	topPanel.add(drinkSelectionBox);
	topPanel.add(belowPanel);

	this.setLayout(new BorderLayout());
	this.add(topPanel);

	pack();
	setLocation(400, 50);

	addWindowListener(this.presenter.getClosingListener());
    }

    private void initialize()
    {
	coinInputBox = new CoinInputBox(presenter);
	coinInputBox.active(false);

	drinkSelectionBox = new DrinkSelectionBox(presenter);

	changeStatusLabel = new Label("No change available.");
	changeStatusLabel.setAlignment(Label.CENTER);
	changeStatusLabel.setBackground(Color.white);
	changeStatusLabel.setForeground(Color.white);

	terminateButton = new Button("Terminate and Return Cash");
	terminateButton.setForeground(Color.pink);
	terminateButton.setBackground(Color.darkGray);
	terminateButton.addActionListener(this.presenter.getTerminateListener());
	collectCoinLabel = new LabelledDisplay("Collect Coins: ", 30,
		LabelledDisplay.GRID);
	collectCoinLabel.setEditable(false);
	collectCoinLabel.setTextBackground(Color.black);
	collectCoinLabel.setTextForeground(Color.white);
	collectCoinLabel.setValue("");

	collectCanLabel = new LabelledDisplay("Collect Can Here: ", 30,
		LabelledDisplay.GRID);
	collectCanLabel.setEditable(false);
	collectCanLabel.setTextBackground(Color.black);
	collectCanLabel.setTextForeground(Color.white);
	collectCanLabel.setValue("");
    }

    @Override
    public void closeDown()
    {
	dispose();
    }

    @Override
    public void display()
    {
	this.setVisible(true);
    }

    @Override
    public void setTotalMoneyInserted(int totalMoneyInserted)
    {
	this.coinInputBox.setTotalMoneyInserted(totalMoneyInserted);
    }

    @Override
    public int getTotalMoneyInserted()
    {
	return coinInputBox.getTotalMoneyInserted();
    }

    @Override
    public void showInvalidCoinStatus()
    {
	this.coinInputBox.showCoinStatus();
    }

    @Override
    public void hideInvalidCoinStatus()
    {
	this.coinInputBox.hideCoinStatus();
    }

    @Override
    public void setCoinInputBoxStatus(boolean status)
    {
	this.coinInputBox.active(status);
    }

    @Override
    public void refresh()
    {
	initialize();
    }

    @Override
    public void setDrinkSelectionBoxStatus(boolean status,
	    String currentItemName)
    {
	this.drinkSelectionBox.setStatus(status, currentItemName);
    }

    @Override
    public void reset()
    {
	this.coinInputBox.reset();
	this.drinkSelectionBox.reset();
	changeStatusLabel.setBackground(Color.white);
	changeStatusLabel.setForeground(Color.white);
	collectCoinLabel.setValue("");
	collectCanLabel.setValue("");
    }

    @Override
    public int getSelectedItemPrice(String selectedItem) // i know it's not a good way. but no time!
    {
	return drinkSelectionBox.getSelectedItemPrice(selectedItem);
    }
    
    @Override
    public void setCoinCollectionLabel(String amt)
    {
	collectCoinLabel.setValue(amt);
    }
    
    @Override
    public void setCanCollectionLabel(String amt)
    {
	collectCanLabel.setValue(amt);
    }
    
    @Override 
    public void dispense(String itemName){
	collectCanLabel.setValue(1);
	this.drinkSelectionBox.dispense(itemName);
    }
}
